﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using jaysJourney_v01.Common;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using jaysJourney_v01.Utils;

namespace jaysJourney_v01.States
{
    class PauseState : State
    {
        /**
         * MEMBER VARIABLES
         * */

        // Common
        private SpriteBatch mainSpriteBatch;
        private InputGroup menuButtonGroup;

        private Texture2D interfaceBG;

        // Buttons
        private Button continueBtn;
        private Button restart;
        private Button loadWorld;
        private Button settings;
        private Button mainMenu;

        /**
         * METHODS
         * */

        public PauseState(String name, StateManager stateManager) : base(name, stateManager) { }

        public override void initialize()
        {
            mainSpriteBatch = this.parentSpriteBatch;

            this.interfaceBG = this.getGame().Content.Load<Texture2D>(@"Textures\Interface\main_menu_v2");

            // Buttons
            this.continueBtn = new Button(this, "CONTINUE", @"Textures\Interface\menu_button", @"Textures\Interface\menu_button_ac", 350, Color.White, true, true);
            this.restart = new Button(this, "RESTART", @"Textures\Interface\menu_button", @"Textures\Interface\menu_button_ac", 450, Color.White, false, true);
            this.loadWorld = new Button(this, "LOAD WORLD", @"Textures\Interface\menu_button", @"Textures\Interface\menu_button_ac", 550, Color.White, false, true);
            this.settings = new Button(this, "SETTINGS", @"Textures\Interface\menu_button", @"Textures\Interface\menu_button_ac", 650, Color.White, false, true);
            this.mainMenu = new Button(this, "MAIN MENU", @"Textures\Interface\back_btn", @"Textures\Interface\back_btn_ac", 20, this.getGame().GraphicsDevice.Viewport.Height - 74, Color.White, false, false);

            this.menuButtonGroup = new InputGroup(this, this.mainSpriteBatch);
            this.menuButtonGroup.addButton(continueBtn);
            this.menuButtonGroup.addButton(restart);
            this.menuButtonGroup.addButton(loadWorld);
            this.menuButtonGroup.addButton(settings);
            this.menuButtonGroup.addButton(mainMenu);
        }

        public override void update(GameTime gameTime)
        {


            // update buttons
            Button click = this.menuButtonGroup.update(gameTime);

            if (click != null && click.getName() == "CONTINUE") this.getStateManager().setActiveState("playState");
            else if (click != null && click.getName() == "RESTART"){
                ((PlayState)this.getStateManager().getState("playState")).resetLevel();
                this.getStateManager().setActiveState("playState");
            }
            else if (click != null && click.getName() == "LOAD WORLD")
            {
                ((LoadWorldState)this.getStateManager().getState("loadWorldState")).loadworldoverview=true;
                this.getStateManager().setActiveState("loadWorldState");
            }
            else if (click != null && click.getName() == "MAIN MENU") this.getStateManager().setActiveState("menuState");
            else if (click != null && click.getName() == "SETTINGS") this.getStateManager().setActiveState("settingsState");

            else if (GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.B)) this.getStateManager().setActiveState("playState");
        }

        public override void draw(GameTime gameTime)
        {
            // draw bg
            this.mainSpriteBatch.Draw(this.getGame().menuBg, new Rectangle(0, 0, this.getGame().GraphicsDevice.Viewport.Width, this.getGame().GraphicsDevice.Viewport.Height), new Rectangle(0, 0, this.getGame().menuBg.Width, this.getGame().menuBg.Height), Color.White);

            // draw interface bg
            this.mainSpriteBatch.Draw(this.interfaceBG, new Rectangle((this.getGame().GraphicsDevice.Viewport.Width / 2) - this.interfaceBG.Width / 2, 0, this.interfaceBG.Width, this.interfaceBG.Height), new Rectangle(0, 0, this.interfaceBG.Width, this.interfaceBG.Height), Color.White);

            // draw buttons
            this.menuButtonGroup.draw(gameTime);
        }

        public override void onEnterState()
        {
            this.continueBtn.setNewPosition(350);
            this.restart.setNewPosition(450);
            this.loadWorld.setNewPosition(550);
            this.settings.setNewPosition(650);
            this.mainMenu.setNewPosition(20, this.getGame().GraphicsDevice.Viewport.Height - 74);
        }
    }
}
